package com.tom.architect.party3.day23;

import com.tom.architect.party3.day23.net.BaseJsonServlet;
import com.tom.architect.party3.day23.net.HttpServletRequest;
import com.tom.architect.party3.day23.net.HttpServletResponse;
import com.tom.architect.party3.day23.response.entity.ResponseEntity;
import com.tom.architect.party3.day23.response.entity.UserInfoEntity;

import okhttp3.Cookie;

// 登录的Servlet
// @WebServlet("/LoginServlet")
public class LoginServlet extends BaseJsonServlet {
    @Override
    protected ResponseEntity onHandler(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        // Integer.parseInt("asdf"); // 模拟错误，因为BaseJsonServlet 已经做了处理。

        ResponseEntity responseEntity = new ResponseEntity();
        responseEntity.code = "0022";
        responseEntity.message= "用户名或密码错误";
//        String userName = req.getParameter("userName"); // 获取登录请求的用户名。
//        if ("Tom".equals(userName)) {
//            responseEntity.code = "0000";
//            responseEntity.message= "登录成功";
//            UserInfoEntity infoEntity = new UserInfoEntity();
//            infoEntity.userName = userName;
//            infoEntity.userSex = "男";
//            responseEntity.data = JSON.toJSONString(infoEntity); // json格式的用户信息
//
//            // 登录成功，带cookie给app端。
//            Cookie cookie = new Cookie("userName", "Tom");
//            cookie.setMaxAge(60*60*24); // 设置登录Cookie的过期时间: 1天
//            resp.addCookie();
//        }
//          else 登录失败
        return responseEntity;
    }
}

// web访问： 192.168.8.169:8080/OKHttpServer/LoginServlet?userName=Tom


